package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.wephone.Student.R;
import com.wephone.Student.bean.AddBlackUserListResponse;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.DeleteCareUserResponse;
import com.wephone.Student.bean.IsCareUserResponse;
import com.wephone.Student.bean.LiuxuequanPublicResponse;
import com.wephone.Student.bean.UserBlackListResponse;
import com.wephone.Student.bean.UserInfoResponse;
import com.wephone.Student.bean.ZhidaoAskListResponse;
import com.wephone.Student.bean.data.CarInfoData;
import com.wephone.Student.bean.data.FlyTogetherInfoData;
import com.wephone.Student.bean.data.HouseInfoData;
import com.wephone.Student.bean.data.LiuxuequanInfoData;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.bean.data.RecruitInfoData;
import com.wephone.Student.bean.data.SecondInfoData;
import com.wephone.Student.bean.data.UserBlackListData;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.bean.data.ZhidaoAskData;
import com.wephone.Student.huanxin.activity.ChatActivity;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;
import com.wephone.Student.util.Util;

public class UserInfoDetailActivity extends BaseActivity {
	private UserInfoData userData;
	private TextView topBarTitle;
	private TextView detailText5;
	private TextView detailText6;
	private TextView detailText7;
	private TextView detailText8;
	private LinearLayout subPicLayout;
	private boolean isCared = false;
	private boolean isAddBlackList = false;
	private ImageView subPic1;
	private ImageView subPic2;
	private ImageView subPic3;
	private ImageView userFlagImage;
	private LinearLayout myTieziLayout;

	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(UserInfoDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					isCared = true;
					findViewById(R.id.btn_bottom_item_1).setBackgroundResource(R.drawable.btn_user_info_detail_bottom_1_2_selected);
					Toast.makeText(getApplicationContext(), "关注成功", Toast.LENGTH_SHORT).show();
					
//					BaseActivity.getMyInfo(getApplicationContext()).setMycare(""+(Integer.parseInt(BaseActivity.getMyInfo(getApplicationContext()).getMycare())+1));
//					ShareReferenceUtil.saveUserInfoJson(getApplicationContext(), gson.toJson(BaseActivity.getMyInfo(getApplicationContext())));
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler2(){
		MyResponseHandler handler = new MyResponseHandler(UserInfoDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					isAddBlackList = true;
					findViewById(R.id.btn_bottom_item_3).setBackgroundResource(R.drawable.btn_cancel_add_black_list_selected);
					Toast.makeText(getApplicationContext(), "添加黑名单成功", Toast.LENGTH_SHORT).show();
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler3(){
		MyResponseHandler handler = new MyResponseHandler(UserInfoDetailActivity.this,IsCareUserResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					IsCareUserResponse careRes = (IsCareUserResponse)response;
					if(careRes.getResult()!=null && !careRes.getResult().equals("0")){
						isCared = true;
						findViewById(R.id.btn_bottom_item_1).setBackgroundResource(R.drawable.btn_user_info_detail_bottom_1_2_selected);
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler4(){
		MyResponseHandler handler = new MyResponseHandler(UserInfoDetailActivity.this,DeleteCareUserResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					DeleteCareUserResponse careRes = (DeleteCareUserResponse)response;
					if(careRes.getResult()!=null && !careRes.getResult().equals("0")){
						isCared = false;
						findViewById(R.id.btn_bottom_item_1).setBackgroundResource(R.drawable.btn_user_info_detail_bottom_1_selected);
						Toast.makeText(getApplicationContext(), "取消关注成功", Toast.LENGTH_SHORT).show();
						
//						BaseActivity.getMyInfo(getApplicationContext()).setMycare(""+(Integer.parseInt(BaseActivity.getMyInfo(getApplicationContext()).getMycare())-1));
//						ShareReferenceUtil.saveUserInfoJson(getApplicationContext(), gson.toJson(BaseActivity.getMyInfo(getApplicationContext())));
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler5(){
		MyResponseHandler handler = new MyResponseHandler(UserInfoDetailActivity.this,UserBlackListResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					UserBlackListResponse blackRes = (UserBlackListResponse)response;
					if(blackRes.getData()==null || blackRes.getData().size()==0){
//						MyMessageActivity.toUserId = userData.getuId();
//						MyMessageActivity.toUserName = userData.getuName();
//						MyMessageActivity.toUserInfo = userData;
//						startActivity(new Intent(getApplicationContext(),MyMessageActivity.class));

						ChatActivity.toUserInfo = userData;
						Intent intent = new Intent(getApplicationContext(),ChatActivity.class);
						intent.putExtra("chatType", ChatActivity.CHATTYPE_SINGLE);
						intent.putExtra("userId", userData.getuId());
						startActivity(intent);
					}else{
						boolean isFound = false;
						for(UserBlackListData data:blackRes.getData()){
							if(data.getBlackUId().equals(userData.getuId())){
								isFound = true;
								break;
							}
						}
						if(isFound){
							Toast.makeText(getApplicationContext(), "你在对方的黑名单内，暂时不能聊天", Toast.LENGTH_SHORT).show();
						}else{
//							MyMessageActivity.toUserId = userData.getuId();
//							MyMessageActivity.toUserName = userData.getuName();
//							MyMessageActivity.toUserInfo = userData;
//							startActivity(new Intent(getApplicationContext(),MyMessageActivity.class));
							
							ChatActivity.toUserInfo = userData;
							Intent intent = new Intent(getApplicationContext(),ChatActivity.class);
							intent.putExtra("chatType", ChatActivity.CHATTYPE_SINGLE);
							intent.putExtra("userId", userData.getuId());
							startActivity(intent);
						}
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler6(){
		MyResponseHandler handler = new MyResponseHandler(UserInfoDetailActivity.this,AddBlackUserListResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					AddBlackUserListResponse blackRes = (AddBlackUserListResponse)response;
					if(blackRes.getResult()!=null && blackRes.getResult().equals("1")){
						isAddBlackList = true;
						findViewById(R.id.btn_bottom_item_3).setBackgroundResource(R.drawable.btn_cancel_add_black_list_selected);
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler7(){
		MyResponseHandler handler = new MyResponseHandler(UserInfoDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					isAddBlackList = false;
					findViewById(R.id.btn_bottom_item_3).setBackgroundResource(R.drawable.btn_user_info_detail_bottom_3_selected);
					Toast.makeText(getApplicationContext(), "删除黑名单成功", Toast.LENGTH_SHORT).show();
				}
			}
		};
		return handler;
	}
	
	private AsyncHttpResponseHandler getHandler8(){
		AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String result) {
//				System.out.println("---result success---"+result);
				if(!TextUtils.isEmpty(result)){
					try{
						Gson gson = new Gson();
						ZhidaoAskListResponse res = (ZhidaoAskListResponse)gson.fromJson(result, ZhidaoAskListResponse.class);
						if(res.getErrNo()!=null && res.getErrNo().equals("0")){
							if(res.getData()!=null){
								if(res.getData().size()>0){
									myTieziLayout.removeAllViews();
									addTieziItemView(res.getData().get(0));
								}
								if(res.getData().size()>1){
									addTieziItemView(res.getData().get(1));
								}
								if(res.getData().size()>2){
									addTieziItemView(res.getData().get(2));
								}
//								if(res.getData().size()>3){
//									addTieziItemView(res.getData().get(3));
//								}
//								if(res.getData().size()>4){
//									addTieziItemView(res.getData().get(4));
//								}
							}
						}
					}catch(Exception e){
						e.printStackTrace();
					}
				}
			}
		};
		return handler;
	}
	
	private MyResponseHandler getHandler9(){
		MyResponseHandler handler = new MyResponseHandler(UserInfoDetailActivity.this,LiuxuequanPublicResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					LiuxuequanPublicResponse res = (LiuxuequanPublicResponse)response;
					if(res.getData()!=null && res.getData().size()>0){
						//TODO
						findViewById(R.id.liuxuequan_status_layout).setVisibility(View.VISIBLE);
						findViewById(R.id.liuxuequan_status_layout).setTag(res.getData().get(0));
						findViewById(R.id.liuxuequan_status_layout).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								LiuxuequanInfoData infoData = (LiuxuequanInfoData)v.getTag();
								startToDetailPage(infoData);
							}
						});
						SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
						TextView itemTime = (TextView)findViewById(R.id.liuxuequan_status_create_time_text);
						itemTime.setText(sdf.format(new Date(Long.parseLong(res.getData().get(0).getCtime())*1000)));
						TextView questionAskName = (TextView)findViewById(R.id.liuxuequan_status_user_name_text);
						if(res.getData().get(0).getType()!=null){
							if(res.getData().get(0).getType().equals("0")){
								questionAskName.setText("发布了山楂圈信息");
							}else if(res.getData().get(0).getType().equals("1")){
								questionAskName.setText("发布了汽车交易信息");
							}else if(res.getData().get(0).getType().equals("2")){
								questionAskName.setText("发布了二手交易信息");
							}else if(res.getData().get(0).getType().equals("3")){
								questionAskName.setText("发布了房屋信息");
							}else if(res.getData().get(0).getType().equals("4")){
								questionAskName.setText("发布了招聘信息");
							}else if(res.getData().get(0).getType().equals("5")){
								questionAskName.setText("发布了山楂知道");
							}else if(res.getData().get(0).getType().equals("6")){
								questionAskName.setText("发布了一起飞信息");
							}
						}
					}else{
						findViewById(R.id.liuxuequan_status_layout).setVisibility(View.VISIBLE);
						findViewById(R.id.liuxuequan_status_create_time_text).setVisibility(View.GONE);
						((TextView)findViewById(R.id.liuxuequan_status_user_name_text)).setText("没有发布信息");
					}
					RoundImageView itemImage = (RoundImageView)findViewById(R.id.liuxuequan_status_image);
					if(!TextUtils.isEmpty(userData.getuIcon())){
						DisplayImageOptions options = new DisplayImageOptions.Builder()
				        .showStubImage(R.drawable.default_user_logo)
				        .showImageOnFail(R.drawable.default_user_logo)
				        .bitmapConfig(Bitmap.Config.ARGB_8888)
				        .cacheInMemory()
				        .cacheOnDisc()
//				        .displayer(new RoundedBitmapDisplayer(1000))
				        .build();
						String endStr = userData.getuIcon().substring(userData.getuIcon().lastIndexOf("."));
						imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
					}else{
						itemImage.setImageResource(R.drawable.default_user_logo);
					}
				}
			}
		};
		return handler;
	}
	
	private AsyncHttpResponseHandler uidHandler = new AsyncHttpResponseHandler(){
		@Override
		public void onSuccess(String result) {
			super.onSuccess(result);
			
//			System.out.println("----on get user info success----"+result);
			if(!TextUtils.isEmpty(result)){
				try{
					Gson gson = new Gson();
					UserInfoResponse response = gson.fromJson(result, UserInfoResponse.class);
					if(response.getErrNo()!=null && response.getErrNo().equals("0")){
						UserInfoData userInfo = response.getData();
						if(!TextUtils.isEmpty(userInfo.getMycare())){
							TextView guanzhuText = (TextView)findViewById(R.id.guanzhu_text);
							guanzhuText.setText("关注"+userInfo.getMycare());
//							if(userInfo.getMycare()!=null && Integer.parseInt(userInfo.getMycare())>0){
								guanzhuText.setTag(userInfo);
								findViewById(R.id.relCareme).setTag(userInfo);
								findViewById(R.id.relCareme).setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View arg0) {
										UserInfoData userInfo = (UserInfoData)arg0.getTag();
										
										Bundle bundle = new Bundle();
										bundle.putString("uid", userInfo.getuId());
										bundle.putString("username", userInfo.getuName());
										bundle.putBoolean("isShowCareMe", true);
										startActivity(new Intent(getApplicationContext(),CareMeOrMyCareListActivity.class).putExtras(bundle));
									}
								});
								guanzhuText.setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View arg0) {
										UserInfoData userInfo = (UserInfoData)arg0.getTag();
										
										Bundle bundle = new Bundle();
										bundle.putString("uid", userInfo.getuId());
										bundle.putString("username", userInfo.getuName());
										bundle.putBoolean("isShowCareMe", false);
										startActivity(new Intent(getApplicationContext(),CareMeOrMyCareListActivity.class).putExtras(bundle));
									}
								});
//							}
						}
						if(!TextUtils.isEmpty(userInfo.getCareme())){
							TextView fensiText = (TextView)findViewById(R.id.fensi_text);
							fensiText.setText("粉丝"+userInfo.getCareme());
//							if(userInfo.getCareme()!=null && Integer.parseInt(userInfo.getCareme())>0){
							findViewById(R.id.relFans).setTag(userInfo);
							findViewById(R.id.relFans).setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View arg0) {
										UserInfoData userInfo = (UserInfoData)arg0.getTag();
										
										Bundle bundle = new Bundle();
										bundle.putString("uid", userInfo.getuId());
										bundle.putString("username", userInfo.getuName());
										bundle.putBoolean("isShowCareMe", false);
										startActivity(new Intent(getApplicationContext(),CareMeOrMyCareListActivity.class).putExtras(bundle));
									}
								});
								fensiText.setTag(userInfo);
								fensiText.setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View arg0) {
										UserInfoData userInfo = (UserInfoData)arg0.getTag();
										
										Bundle bundle = new Bundle();
										bundle.putString("uid", userInfo.getuId());
										bundle.putString("username", userInfo.getuName());
										bundle.putBoolean("isShowCareMe", true);
										startActivity(new Intent(getApplicationContext(),CareMeOrMyCareListActivity.class).putExtras(bundle));
									}
								});
//							}
						}
						((TextView)findViewById(R.id.group_text)).setText("群组"+(userInfo.getMycreate()+userInfo.getMyjoin()));
						findViewById(R.id.group_text).setTag(userInfo);
						findViewById(R.id.relGroup).setTag(userInfo);
						findViewById(R.id.relGroup).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View arg0) {
								UserInfoData userInfo = (UserInfoData)arg0.getTag();
								Bundle bundle = new Bundle();
								bundle.putString("uid", userInfo.getuId());
								startActivity(new Intent(getApplicationContext(),MyGroupsActivity.class).putExtras(bundle));
							}
						});
						findViewById(R.id.group_text).setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View arg0) {
								UserInfoData userInfo = (UserInfoData)arg0.getTag();
								Bundle bundle = new Bundle();
								bundle.putString("uid", userInfo.getuId());
								startActivity(new Intent(getApplicationContext(),MyGroupsActivity.class).putExtras(bundle));
							}
						});
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_info_detail);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			userData = (UserInfoData)bundle.getSerializable("userData");
			init();
		}else{
			finish();
			Toast.makeText(getApplicationContext(), "数据异常", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void init(){
		topBarTitle = (TextView)findViewById(R.id.top_bar_title);
		detailText5 = (TextView)findViewById(R.id.user_info_detail_edit_5);
		detailText6 = (TextView)findViewById(R.id.user_info_detail_edit_6);
		detailText7 = (TextView)findViewById(R.id.user_info_detail_edit_7);
		detailText8 = (TextView)findViewById(R.id.user_info_detail_edit_8);
		subPicLayout = (LinearLayout)findViewById(R.id.user_info_detail_sub_pic_layout);
		subPic1 = (ImageView)findViewById(R.id.user_info_detail_pic_1);
		subPic2 = (ImageView)findViewById(R.id.user_info_detail_pic_2);
		subPic3 = (ImageView)findViewById(R.id.user_info_detail_pic_3);
		userFlagImage = (ImageView)findViewById(R.id.user_info_detail_flag);
		myTieziLayout = (LinearLayout)findViewById(R.id.user_info_my_tiezi_layout);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		TextView guanzhuText = (TextView)findViewById(R.id.guanzhu_text);
		TextView fensiText = (TextView)findViewById(R.id.fensi_text);
		if(!TextUtils.isEmpty(userData.getMycare())){
			guanzhuText.setText("关注"+userData.getMycare());
		}
		if(!TextUtils.isEmpty(userData.getCareme())){
			fensiText.setText("粉丝"+userData.getCareme());
		}
		TextView genderText = (TextView)findViewById(R.id.item_gender_text);
		genderText.setText(userData.getuAge());
		topBarTitle.setText("个人信息");
		TextView userName = (TextView)findViewById(R.id.user_name_text);
		userName.setText(userData.getuName());
		ImageView genderImage = (ImageView)findViewById(R.id.item_gender);
		
//		Toast.makeText(getApplicationContext(), BaseActivity.myInfo.getuBirthday(), Toast.LENGTH_LONG).show();
//		System.out.println("---birth---"+userData.getuBirthday());
//		if(userData.getuBirthday().contains("-")){
//			try {
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//				Date date = sdf.parse(userData.getuBirthday());
//				Calendar c = Calendar.getInstance();
//				c.setTime(date);
////				System.out.println("_year:"+_year+" _month:"+_month+" _day:"+_day);
//				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
//				detailText3.setText(sdf2.format(date));
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}else{
//			String birthStr = userData.getuBirthday();
//			int birthLength = birthStr.length();
//			for(int i=0;i<(12-birthLength);i++){
//				birthStr = birthStr+"0";
//			}
//			Date userDate = new Date(Long.parseLong(birthStr));
//			Calendar c = Calendar.getInstance();
//			c.setTime(userDate);
//			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日");
//			detailText3.setText(sdf2.format(userDate));
//		}
//		System.out.println("-----locDetail-----"+userData.getuLocDetail());
		if(!TextUtils.isEmpty(userData.getuLocDetail())){
			Gson gson = new Gson();
			MyLocationInfo locInfo = gson.fromJson(userData.getuLocDetail(), MyLocationInfo.class);
			if(locInfo.getType().equals("PlanGoOut")){
				detailText7.setText("想去"+locInfo.getToschool());
				detailText5.setText("中国 "+locInfo.getCity());
				
				userFlagImage.setImageResource(Util.getCountryFlagSrc("中国 "));
			}else if(locInfo.getType().equals("Returnees")){
				if(!TextUtils.isEmpty(locInfo.getSchool2())){
					detailText7.setText("曾在"+locInfo.getSchool1()+","+locInfo.getSchool2());
				}else{
					detailText7.setText("曾在"+locInfo.getSchool1());
				}
				detailText5.setText(locInfo.getLoc());
				
				userFlagImage.setImageResource(Util.getCountryFlagSrc(locInfo.getCountry()));
			}else{
				detailText7.setText(locInfo.getSchool());
				detailText5.setText(locInfo.getCity()+" "+locInfo.getCountry());
				
				userFlagImage.setImageResource(Util.getCountryFlagSrc(locInfo.getCountry()));
			}
//			if(!TextUtils.isEmpty(locInfo.getLoc())){
//				detailText5.setText(locInfo.getLoc());
//			}else{
//				detailText5.setText(locInfo.getCity());
//			}
			detailText6.setText(locInfo.getHome());
		}
//		detailText4.setText(userData.getuConstellation());
		detailText8.setText(userData.getuSign());
		
		RoundImageView itemImage = (RoundImageView)findViewById(R.id.user_info_detail_user_logo);
		if(!TextUtils.isEmpty(userData.getuIcon())){
			DisplayImageOptions options = new DisplayImageOptions.Builder()
	        .showStubImage(R.drawable.default_user_logo)
	        .showImageOnFail(R.drawable.default_user_logo)
	        .bitmapConfig(Bitmap.Config.ARGB_8888)
	        .cacheInMemory()
	        .cacheOnDisc()
//	        .displayer(new RoundedBitmapDisplayer(1000))
	        .build();
//			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuIcon(), itemImage, options);
			String endStr = userData.getuIcon().substring(userData.getuIcon().lastIndexOf("."));
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
		}
		itemImage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showBigPictureDialog(getApplicationContext(), 99);
			}
		});
		
		if(TextUtils.isEmpty(userData.getuImg1()) && TextUtils.isEmpty(userData.getuImg2()) && TextUtils.isEmpty(userData.getuImg3())){
			subPicLayout.setVisibility(View.GONE);
		}else{
			if(!TextUtils.isEmpty(userData.getuImg1())){
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg1(), subPic1);
//				String endStr = userData.getuImg1().substring(userData.getuImg1().lastIndexOf("."));
//				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg1().replace(endStr, "_150_150"+endStr),subPic1);
				subPic1.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						showBigPictureDialog(getApplicationContext(), 0);
					}
				});
			}else{
				subPic1.setVisibility(View.GONE);
			}
			if(!TextUtils.isEmpty(userData.getuImg2())){
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg2(), subPic2);
//				String endStr = userData.getuImg2().substring(userData.getuImg2().lastIndexOf("."));
//				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg2().replace(endStr, "_150_150"+endStr),subPic2);
				subPic2.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						showBigPictureDialog(getApplicationContext(), 1);
					}
				});
			}else{
				subPic2.setVisibility(View.GONE);
			}
			if(!TextUtils.isEmpty(userData.getuImg3())){
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg3(), subPic3);
//				String endStr = userData.getuImg3().substring(userData.getuImg3().lastIndexOf("."));
//				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg3().replace(endStr, "_150_150"+endStr),subPic3);
				subPic3.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						showBigPictureDialog(getApplicationContext(), 2);
					}
				});
			}else{
				subPic3.setVisibility(View.GONE);
			}
		}
		
		if(userData.getuId().equals(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
			((TextView)findViewById(R.id.top_bar_title)).setText("我的山楂圈");
			findViewById(R.id.detail_command_bottom_layout).setVisibility(View.GONE);
			findViewById(R.id.settings_other_tiezi_layout).setVisibility(View.GONE);
			findViewById(R.id.settings_liuxuequan_status_layout).setVisibility(View.GONE);
			findViewById(R.id.settings_tiezi_layout).setVisibility(View.VISIBLE);
			findViewById(R.id.settings_tiezi_layout).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Bundle bundle = new Bundle();
					bundle.putString("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
					startActivity(new Intent(getApplicationContext(),ZhidaoMyTieziListActivity.class).putExtras(bundle));
				}
			});
			findViewById(R.id.settings_shoucang_layout).setVisibility(View.VISIBLE);
			findViewById(R.id.settings_shoucang_layout).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					startActivity(new Intent(getApplicationContext(),ZhidaoMyShoucangListActivity.class));
				}
			});
			findViewById(R.id.settings_liuxuequan_layout).setVisibility(View.VISIBLE);
			findViewById(R.id.settings_liuxuequan_layout).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					startActivity(new Intent(getApplicationContext(),LiuXueQuanDetailActivity.class).putExtra("userInfo", userData));
				}
			});
		}else{
			if(userData.getuGender().equals("0")){
				genderImage.setImageResource(R.drawable.online_male);
				((TextView)findViewById(R.id.top_bar_title)).setText("他的山楂圈");
				((TextView)findViewById(R.id.liuxuequan_status_title_text)).setText("他的山楂圈");
				((TextView)findViewById(R.id.other_tiezi_title_text)).setText("他的帖子");
			}else{
				genderImage.setImageResource(R.drawable.online_female);
				((TextView)findViewById(R.id.top_bar_title)).setText("她的山楂圈");
				((TextView)findViewById(R.id.liuxuequan_status_title_text)).setText("她的山楂圈");
				((TextView)findViewById(R.id.other_tiezi_title_text)).setText("她的帖子");
			}
			findViewById(R.id.settings_other_tiezi_layout).setVisibility(View.VISIBLE);
			findViewById(R.id.settings_other_tiezi_layout).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Bundle bundle = new Bundle();
					bundle.putString("uid", userData.getuId());
					bundle.putString("username", userData.getuName());
					startActivity(new Intent(getApplicationContext(),ZhidaoMyTieziListActivity.class).putExtras(bundle));
				}
			});
			findViewById(R.id.settings_liuxuequan_status_layout).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					startActivity(new Intent(getApplicationContext(),LiuXueQuanDetailActivity.class).putExtra("userInfo", userData));
				}
			});
			findViewById(R.id.btn_bottom_item_1).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if(isCared){
						RequestParams params = new RequestParams();
						params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						params.put("careUId", userData.getuId());
						HttpClient.getInstance().get(Constants.API_DELETE_CARE_USER_URL, params, getHandler4());
					}else{
						RequestParams params = new RequestParams();
						params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						params.put("careUId", userData.getuId());
						HttpClient.getInstance().post(Constants.API_ADD_CARE_USER_URL, params, getHandler());
					}
				}
			});
			findViewById(R.id.btn_bottom_item_2).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if(isAddBlackList){
						Toast.makeText(getApplicationContext(), "对方在你的黑名单内，暂时不能聊天", Toast.LENGTH_SHORT).show();
					}else{
						RequestParams params = new RequestParams();
						params.put("uId", userData.getuId());
						params.put("Page", "1");
						params.put("PageSize", "100");
						HttpClient.getInstance().get(Constants.API_GET_BLACK_USER_LIST_URL, params, getHandler5());
					}
				}
			});
			findViewById(R.id.btn_bottom_item_3).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					if(isAddBlackList){
						RequestParams params2 = new RequestParams();
						params2.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						params2.put("blackUId", userData.getuId());
						HttpClient.getInstance().get(Constants.API_DELETE_BLACK_USER_URL, params2, getHandler7());
					}else{
						RequestParams params = new RequestParams();
						params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
						params.put("blackUId", userData.getuId());
						HttpClient.getInstance().post(Constants.API_ADD_BLACK_USER_URL, params, getHandler2());
					}
				}
			});
			
			RequestParams params = new RequestParams();
			params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
			params.put("careUId", userData.getuId());
			HttpClient.getInstance().get(Constants.API_IS_CARE_USER_URL, params, getHandler3());
			
			RequestParams params2 = new RequestParams();
			params2.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
			params2.put("blackUId", userData.getuId());
			HttpClient.getInstance().get(Constants.API_IS_BLACK_USER_URL, params2, getHandler6());
			
			//我的帖子列表
//			RequestParams params3 = new RequestParams();
//			params3.put("uid", userData.getuId());
//			params3.put("Page", "1");
//			params3.put("PageSize", ""+Constants.PAGE_SIZE);
//			HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_MY_ASK_LIST_URL, params3, getHandler8());
			
		}
		
		//liuxuequan status
		RequestParams params4 = new RequestParams();
		params4.put("uId", userData.getuId());
		params4.put("Page", "1");
		params4.put("PageSize", "1");
		HttpClient.getInstance().get(Constants.API_ZHIDAO_GET_MY_STUDY_CIRCLE_URL, params4, getHandler9());
		
		RequestParams params5 = new RequestParams();
		params5.put("uid", userData.getuId());
		HttpClient.getInstance().get(Constants.API_GET_USER_INFO_URL, params5, uidHandler);
		
		/*
		RequestParams params3 = new RequestParams();
		params3.put("uId", userData.getuId());
		HttpClient.getInstance().get(Constants.API_GET_CHART_COUNT, params3, new MyResponseHandler(UserInfoDetailActivity.this, BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				try {
					JSONObject object = new JSONObject(result);
					JSONObject dataObject = object.getJSONObject("data");
					int mycreate = dataObject.getInt("mycreate");
					int myjoin = dataObject.getInt("myjoin");
					((TextView)findViewById(R.id.group_text)).setText("群组"+(mycreate+myjoin));
					findViewById(R.id.relGroup).setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							Bundle bundle = new Bundle();
							bundle.putString("uid", userData.getuId());
							startActivity(new Intent(getApplicationContext(),MyGroupsActivity.class).putExtras(bundle));
						}
					});
					findViewById(R.id.group_text).setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View arg0) {
							Bundle bundle = new Bundle();
							bundle.putString("uid", userData.getuId());
							startActivity(new Intent(getApplicationContext(),MyGroupsActivity.class).putExtras(bundle));
						}
					});
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			@Override
			public void onFailure(Throwable arg0, String result) {
				super.onFailure(arg0,result);
			}
		});
		*/
	}

	private void showBigPictureDialog(Context context,final int idx){
		final View inputView = getLayoutInflater().inflate(R.layout.dialog_show_big_picture_view, null);  
		ImageView bigImage = (ImageView)inputView.findViewById(R.id.item_big_image);
		if(idx==0){
			if(!TextUtils.isEmpty(userData.getuImg1())){
				String bigImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg1(); 
				startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
			}else{
				return;
			}
		}else if(idx==1){
			if(!TextUtils.isEmpty(userData.getuImg2())){
				String bigImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg2(); 
				startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
			}else{
				return;
			}
		}else if(idx==2){
			if(!TextUtils.isEmpty(userData.getuImg3())){
				String bigImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuImg3(); 
				startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
			}else{
				return;
			}
		}else if(idx==99){
			if(!TextUtils.isEmpty(userData.getuIcon())){
				String bigImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+userData.getuIcon(); 
				startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
			}else{
				return;
			}
		}
	}
	
	private void addTieziItemView(ZhidaoAskData askData){
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		View convertView = getLayoutInflater().inflate(R.layout.include_user_info_detail_my_tiezi_item, null);
		TextView itemTime = (TextView)convertView.findViewById(R.id.create_time_text);
		itemTime.setText(sdf.format(new Date(Long.parseLong(askData.getCtime())*1000)));
		TextView itemContent = (TextView)convertView.findViewById(R.id.item_content_text);
		if(askData.getTitle().length()>15){
			itemContent.setText(askData.getTitle().substring(0, 13)+"...");
		}else{
			itemContent.setText(askData.getTitle());
		}
		TextView questionAskName = (TextView)convertView.findViewById(R.id.user_name_text);
		questionAskName.setText(askData.getuInfo().getuName());
		RoundImageView itemImage = (RoundImageView)convertView.findViewById(R.id.question_ask_image);
		if(!TextUtils.isEmpty(askData.getuInfo().getuIcon())){
			DisplayImageOptions options = new DisplayImageOptions.Builder()
	        .showStubImage(R.drawable.default_user_logo)
	        .showImageOnFail(R.drawable.default_user_logo)
	        .bitmapConfig(Bitmap.Config.ARGB_8888)
	        .cacheInMemory()
	        .cacheOnDisc()
//	        .displayer(new RoundedBitmapDisplayer(1000))
	        .build();
			String endStr = askData.getuInfo().getuIcon().substring(askData.getuInfo().getuIcon().lastIndexOf("."));
			imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+askData.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
		}else{
			itemImage.setImageResource(R.drawable.default_user_logo);
		}
		convertView.setTag(askData);
		convertView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				ZhidaoAskData askData = (ZhidaoAskData)arg0.getTag();

				Bundle bundle = new Bundle();
				bundle.putSerializable("askData", askData);
				startActivity(new Intent(getApplicationContext(),ZhidaoAnswerListActivity.class).putExtras(bundle));
			}
		});
		myTieziLayout.addView(convertView);
	}
	
	private DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.liuxuequan_image_default)
    .showImageOnFail(R.drawable.liuxuequan_image_default)
    .bitmapConfig(Bitmap.Config.ARGB_8888)
    .cacheInMemory()
    .cacheOnDisc()
//    .displayer(new RoundedBitmapDisplayer(1000))
    .build();
	
	private void startToDetailPage(LiuxuequanInfoData infoData){
		if(infoData.getType()!=null){
			if(infoData.getType().equals("1")){
				CarInfoData carInfo = infoData.getOtherData().getCarInfoData();
				carInfo.setuInfo(infoData.getuInfo());
				MutualTransaction2DetailActivity.data = carInfo;
				startActivity(new Intent(getApplicationContext(),MutualTransaction2DetailActivity.class));
			}else if(infoData.getType().equals("2")){
				SecondInfoData secondInfo = infoData.getOtherData().getSecondInfoData();
				secondInfo.setuInfo(infoData.getuInfo());
				MutualTransaction1DetailActivity.data = secondInfo;
				startActivity(new Intent(getApplicationContext(),MutualTransaction1DetailActivity.class));
			}else if(infoData.getType().equals("3")){
				HouseInfoData houseInfo = infoData.getOtherData().getHouseInfoData();
				houseInfo.setuInfo(infoData.getuInfo());
				HouseInfoDetailActivity.data = houseInfo;
				startActivity(new Intent(getApplicationContext(),HouseInfoDetailActivity.class));
			}else if(infoData.getType().equals("4")){
				RecruitInfoData recruitInfo = infoData.getOtherData().getRecruitInfoData();
				recruitInfo.setuInfo(infoData.getuInfo());
				MutualTransaction3DetailActivity.data = recruitInfo;
				startActivity(new Intent(getApplicationContext(),MutualTransaction3DetailActivity.class));
			}else if(infoData.getType().equals("5")){
				ZhidaoAskData askData = infoData.getOtherData().getZhidaoAskData();
				askData.setuInfo(infoData.getuInfo());
				Bundle bundle = new Bundle();
				bundle.putSerializable("askData", askData);
				startActivity(new Intent(getApplicationContext(),ZhidaoAnswerListActivity.class).putExtras(bundle));
			}else if(infoData.getType().equals("6")){
				FlyTogetherInfoData flyInfo = infoData.getOtherData().getFlyTogetherInfoData();
				flyInfo.setuInfo(infoData.getuInfo());
				FlyTogetherDetailActivity.data = flyInfo;
				startActivity(new Intent(getApplicationContext(),FlyTogetherDetailActivity.class));
			}else if(infoData.getType().equals("0")){
//				startActivity(new Intent(getApplicationContext(),LiuXueQuanDetailActivity.class).putExtra("userInfo", infoData.getuInfo()));
				Bundle bundle = new Bundle();
				bundle.putSerializable("infoData", infoData);
				startActivity(new Intent(getApplicationContext(),LiuXueQuanPinglunDetailActivity.class).putExtras(bundle));
			}
		}
	}
	
}
